import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useArticleStore = defineStore('article', () => {
  // 文章列表状态
  const articles = ref([]);
  const total = ref(0);
  const page = ref(1);
  const limit = ref(10);
  const totalPages = ref(0);

  // 更新文章列表
  const setArticles = (newArticles) => {
    articles.value = newArticles;
  };

  // 更新总数
  const setTotal = (newTotal) => {
    total.value = newTotal;
  };

  // 更新页码
  const setPage = (newPage) => {
    page.value = newPage;
  };

  // 更新每页数量
  const setLimit = (newLimit) => {
    limit.value = newLimit;
  };

  const setTotalPages = (newTotalPages) => {
    totalPages.value = newTotalPages;
  };

  // 批量更新
  const updateState = (newState) => {
    if (newState.articles !== undefined) articles.value = newState.articles;
    if (newState.total !== undefined) total.value = newState.total;
    if (newState.page !== undefined) page.value = newState.page;
    if (newState.limit !== undefined) limit.value = newState.limit;
    if (newState.totalPages !== undefined) totalPages.value = newState.totalPages;
  };

  return {
    articles,
    total,
    page,
    limit,
    totalPages,
    setArticles,
    setTotal,
    setPage,
    setLimit,
    setTotalPages,
    updateState
  };
});